/* 提交表单 - 添加/修改 */
function onSubmit(options) {
  return new Promise(function(resolve, reject) {
    const _that = options._that
    const formName = options.formName
    let data = options.data ? options.data : _that.list
    const defaultData = options.defaultData ? options.defaultData : _that.defaultList
    _that.$refs[formName].validate((valid) => {
      if (valid) {
        // _that.$confirm('是否提交数据', '提示', {
        //   confirmButtonText: '确定',
        //   cancelButtonText: '取消',
        //   type: 'warning'
        // }).then(() => {
        _that.$nextTick(() => {
          _that.listLoading = true
          if (_that.isEdit) {
            options.updFuc(data).then(response => {
              succFuc(response)
              resolve(response)
            }).catch(error => {
              _that.listLoading = false
              reject(error)
            })
          } else {
            options.addFuc(data).then(response => {
              succFuc(response)
              resolve(response)
            }).catch(error => {
              _that.listLoading = false
              reject(error)
            })
          }
        })
        // }).catch(() => {
        //   _that.$nextTick(() => {
        //     _that.listLoading = false
        //   })
        // })
      } else {
        _that.listLoading = false
        _that.$message({
          message: '提交失败，请完善必填项',
          type: 'error',
          duration: 3000
        })
        return false
      }
    })

    // 请求成功后操作
    function succFuc(response) {
      _that.listLoading = false
      if (response.code === '001') {
        let msg = response.msg ? response.msg : response.message
        if (!msg || msg == "") {
          msg = '操作失败'
        }
        _that.$message({
          message: msg,
          type: 'warning',
          duration: 3000
        })
      } else {
        if (_that.$refs[formName] !== undefined && _that.$refs[formName] !== null) {
          _that.$refs[formName].resetFields()
        }
        if (defaultData !== undefined && defaultData !== null) {
          data = Object.assign({}, defaultData)
        }
        _that.$message({
          message: '操作成功',
          type: 'success',
          duration: 3000
        })
      }
    }
  })
}
/* switch按钮 - 上/下架 */
function switchChange(options) {
  return new Promise(function(resolve, reject) {
    const _that = options._that
    const reqFuc = options.reqFuc
    const row = options.data
    const valName = options.valName
    const value = row[valName]
    const elseData = options.elseData ? options.elseData : {}

    let text = ''
    if (valName === 'upperFlag') {
      text = value === '0' ? '上架该产品?' : value === '1' ? '下架该产品?' : ''
    } else if (valName === 'isMandatory') {
      text = value === 1 ? '必选?' : value === 0 ? '不用必选?' : ''
    } else if (valName === 'showFlag') {
      text = value === '1' ? '显示?' : value === '0' ? '不显示?' : ''
    } else if (valName === 'isValid') {
      text = value === '1' ? '启用?' : value === '0' ? '禁用?' : ''
    } else if (valName === 'isActive') {
      text = value === 1 ? '激活?' : value === 2 ? '屏蔽?' : ''
    } else if (valName === 'isTj') {
      text = value === '1' ? '设置特惠专区?' : value === '0' ? '取消特惠专区?' : ''
    } else if (valName === 'isNew') {
      text = value === '1' ? '设置新品专享?' : value === '0' ? '取消新品专享?' : ''
    } else if (valName === 'status') {
      text = value == 1 ? '开启?' : value == 2 ? '关闭?' : ''
    } else if (valName === 'isHeadline') {
      text = value == 1 ? '开启?' : value == 0 ? '关闭?' : ''
    } else if (valName === 'delFlag') {
      text = value == 0 ? '启用?' : value == 1 ? '禁用?' : ''
    } else if (valName === 'onlineFlag') {
      text = value == 1 ? '上线?' : value == 2 ? '下线?' : ''
    }
    _that.$confirm('确认' + text, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      let data = JSON.parse(JSON.stringify(elseData))
      data.id = row.id
      data[valName] = value
      _that.listLoading = true
      reqFuc(data).then(response => {
        _that.listLoading = false
        _that.$message({
          message: text.replace('?', '') + '成功！',
          type: 'success',
          duration: 3000
        })
        resolve(response)
      }).catch(error => {
        errorDo()
      })
    }).catch(error => {
      errorDo()
    })

    // 操作失败后还原数值
    function errorDo() {
      _that.listLoading = false
      console.log(value)
      if (valName === 'status') {
        value === '1' ? row[valName] = '2' : row[valName] = '1'
      } else if (valName === 'isActive' || valName === 'onlineFlag') {
        value === 1 ? row[valName] = 2 : row[valName] = 1
      } else if (valName === 'isMandatory') {
        value === '1' ? row[valName] = '0' : row[valName] = '1'
      } else {
        value === '0' ? row[valName] = '1' : row[valName] = '0'
      }
      console.log(row[valName])
    }
  })
}

/* 置顶按钮 - 置顶/取消置顶 */
function btnChange(options) {
  return new Promise(function (resolve, reject) {
    const _that = options._that
    const reqFuc = options.reqFuc
    const row = options.data
    const valName = options.valName
    const value = row[valName]

    let text = ''

    if (valName === 'topFlag' || valName === 'isUpper') {
      text = value === '0' ? '置顶该产品?' : value === '1' ? '取消置顶改产品?' : ''
    }

    _that.$confirm('确认' + text, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }).then(() => {
      const data = {
        'id': row.id
      }
      data[valName] = value === '0' ? '1' : '0'

      reqFuc(data).then(response => {
        _that.$message({
          message: text + '置顶成功！',
          type: 'success',
          duration: 3000
        })
        value === '0' ? row[valName] = '1' : row[valName] = '0'
      })
    })
  })
}

export {
  onSubmit,
  switchChange,
  btnChange
}
